package org.baojie.raft.node.type;

import org.baojie.raft.store.NodeStore;

import java.util.concurrent.atomic.AtomicInteger;

public class Candidate extends AbstractRole {

    private final AtomicInteger count;

    public Candidate(NodeStore store, int votes, int term) {
        super(Name.CANDIDATE, store, term);
        if (votes <= 0) {
            this.count = new AtomicInteger(1);
        } else {
            this.count = new AtomicInteger(votes);
        }
    }

    @Override
    public int votedCount() {
        return count.get();
    }

    public void addOneCount() {
        for (; ; ) {
            int tmp = count.get();
            if (count.compareAndSet(tmp, tmp + 1)) {
                return;
            }
        }
    }

}
